<?php
/**
 * 后台用户控制器
 * @author yupoxiong<i@yufuping.com>
 */

namespace app\admin\controller;

use app\admin\model\Ota;
use think\Request;
use app\admin\model\OtaUser;
use app\admin\model\OtaGroup;
use app\admin\validate\OtaUserValidate;

class OtaUserController extends Controller
{

    //列表
    public function index(Request $request, OtaUser $model, Ota $ota)
    {

        $param = $request->param();
        if ($this->user->is_admin == 1) {
            $data = $model->scope('where', $param)
                ->paginate($this->admin['per_page'], false, ['query' => $request->get()]);
        }else{
            $data = $model->scope('where', $param)
                ->where(['account'=>$this->user->account])
                ->paginate($this->admin['per_page'], false, ['query' => $request->get()]);
        }

        $page = $data->render();
        foreach ($data as $k=>$d){
            $data[$k]['groupCount'] = $model->OtaGroup()->where(['ota_account'=>$d['account']])->count();
            $data[$k]['ImeiCount'] = $model->OtaImei()->where(['ota_account'=>$d['account']])->count();
            $data[$k]['OtaCount'] = $ota->where(['ota_account'=>$d['account']])->count();
        }

        //关键词，排序等赋值
        $this->assign($request->get());

        $this->assign([
            'data'  => $data,
            'page'  => $page,
            'total' => $data->total(),
        ]);
        return $this->fetch();
    }

    //添加
    public function add(Request $request, OtaUser $model, OtaGroup $OtaGroup, OtaUserValidate $validate)
    {
        if ($request->isPost()) {
            $param           = $request->param();
            $validate_result = $validate->scene('add')->check($param);
            if (!$validate_result) {
                return admin_error($validate->getError());
            }

            $result = $model::create($param);

            // 该创建用户下无分组时自动生成默认分组
            $OtaGroupCount = $OtaGroup->where(['ota_account'=>$param['account']])->count();

            if ($OtaGroupCount <= 0) {
                $InsertGroup = [
                    'group_name' => '默认分组',
                    'ota_account' => $param['account'],
                    'create_time' => time(),
                    'is_operate' => 0
                ];
                $OtaGroup::create($InsertGroup);
            }

            $url = URL_BACK;
            if (isset($param['_create']) && $param['_create'] == 1) {
                $url = URL_RELOAD;
            }

            return $result ? admin_success('添加成功', $url) : admin_error();
        }

        return $this->fetch();
    }

    //修改
    public function edit($id, Request $request, OtaUser $model, OtaGroup $OtaGroup, OtaUserValidate $validate)
    {

        $data = $model::get($id);
        if ($request->isPost()) {
            $param           = $request->param();
            $validate_result = $validate->scene('edit')->check($param);
            if (!$validate_result) {
                return admin_error($validate->getError());
            }

            $result = $data->save($param);
            return $result ? admin_success() : admin_error();
        }

        $this->assign([
            'data' => $data,
        ]);
        return $this->fetch('add');

    }


    //删除
    public function del($id, OtaUser $model)
    {
        if (count($model->noDeletionId) > 0) {
            if (is_array($id)) {
                if (array_intersect($model->noDeletionId, $id)) {
                    return admin_error('ID为' . implode(',', $model->noDeletionId) . '的数据无法删除');
                }
            } else if (in_array($id, $model->noDeletionId)) {
                return admin_error('ID为' . $id . '的数据无法删除');
            }
        }

        if ($model->softDelete) {
            $result = $model->whereIn('id', $id)->useSoftDelete('delete_time', time())->delete();
        } else {
            $result = $model->whereIn('id', $id)->delete();
        }

        return $result ? admin_success('操作成功', URL_RELOAD) : admin_error();
    }

}
